package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.dto.PageDto;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 12:47
 */
@Service
public class DtsTopicServiceImpl implements DtsTopicService {
    @Autowired
    private DtsTopicMapper dtsTopicMapper;

    @Override
    public Page<DtsTopic> selectPage(PageDto pageDto) {
        // //http://localhost:8080/admin/topic/list?page=1&limit=20&title=ss&subtitle=ss&sort=add_time&order=desc


        Page<DtsTopic> page = dtsTopicMapper.selectPage(
                new Page<DtsTopic>(pageDto.getPage(), pageDto.getLimit()),
                new QueryWrapper<DtsTopic>() .orderBy(!StringUtils.isEmpty(pageDto.getSort()), "asc".equalsIgnoreCase(pageDto.getOrder()), pageDto.getSort())
                        .lambda()
                        .eq(pageDto.getTitle() != null, DtsTopic::getTitle, pageDto.getTitle())
                        .eq(pageDto.getSubtitle() != null, DtsTopic::getSubtitle, pageDto.getSubtitle())
                        .eq(true, DtsTopic::getDeleted, false));

        return page;
    }

    @Override
    public void update(DtsTopic dtsTopic) {
         dtsTopicMapper.updateById(dtsTopic);
    }

    @Override
    public void insert(DtsTopic dtsTopic) {
               dtsTopicMapper.insert(dtsTopic);
    }

    @Override
    public void delete(DtsTopic dtsTopic) {
        dtsTopicMapper.updateById(dtsTopic);
    }
}
